package com.blacktea.cloudproviderhystrixpayment.controller;


import com.blacktea.cloudapicommon.beans.CommonResult;
import com.blacktea.cloudapicommon.beans.Payment;
import com.blacktea.cloudproviderhystrixpayment.service.PaymentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author blacktea
 * @since 2021-07-28
 */
@RestController
@RequestMapping("/payment")
@RequiredArgsConstructor
public class PaymentController {

    private final PaymentService paymentService;
    private final DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPost;

    @PostMapping("/create")
    public CommonResult<Boolean> create(@RequestBody Payment payment){
        boolean save = paymentService.save(payment);
        String result = save?"成功":"失败";
        return new CommonResult<>(200,"port:"+serverPost+",插入数据"+result+"!",save);
    }

    @GetMapping("{id}")
    public CommonResult<Payment> getById(@PathVariable("id") Long id){
        return new CommonResult<>(200,"port:"+serverPost+",查询结果",paymentService.getById(id));
    }

    @PostMapping("/update")
    public CommonResult<Boolean> update(@RequestBody Payment payment){
        return CommonResult.Result(paymentService.updateById(payment));
    }

    @GetMapping("list")
    public CommonResult<List<Payment>> list(){
        return new CommonResult<>(200,"port:"+serverPost+",列表",paymentService.list());
    }

    @PostMapping("del_ids")
    public CommonResult delByIds(List<Long> ids){
        return CommonResult.Result(paymentService.removeByIds(ids));
    }

    @GetMapping("discovery")
    public CommonResult discovery(){
        List<String> services = discoveryClient.getServices();
        return new CommonResult(200,"port:"+serverPost+",服务发现",services);
    }
}

